<?php
namespace Home\Controller;

use Utuo\ForegroundController;

class MyShowController extends ForegroundController {
	
	
	public function _init() {
		
	}
	
	/**
	 * 展示页
	 */
	public function indexAction() {
		$id = I('id', 0, "intval");
		$myShow = M("MyShow a")->join("__MY_SHOW_TPL__ b on a.tplId = b.id")->where("a.cid=%d and a.id=%d", $this->cid, $id)->field("a.*,b.name as tplName")->find();
		$myShow['config'] = unserialize($myShow['config'] );
		$this->assign("myShow", $myShow);
		$this->display("MicroShow/tpl/{$myShow['tplName']}");
	}
	
	/**
	 * 列表页
	 */
	public function listAction() {
		$myShowList = M("MyShow a")->join("__MY_SHOW_TPL__ b on a.tplId = b.id")->where("a.cid=%d", $this->cid)->field("a.*,b.name as tplName,b.image as tplImage")->select();
		$this->display("MicroShow/list");
	}
	
	/**
	 * 编辑页
	 */
	public function modifyAction() {
		$id = I('id', 0, "intval"); // 微秀id
		$myShow = M("MyShow a")->join("__MY_SHOW_TPL__ b on a.tplId = b.id")->where("a.cid=%d and a.id=%d", $this->cid, $id)->field("a.*,b.name as tplName")->find();
		$myShow['config'] = unserialize($myShow['config'] );
		$this->assign("myShow", $myShow);
		$this->assign("mode", "edit");
		$this->display("MicroShow/tpl/{$myShow['tplName']}");
	}
	
	/**
	 * 更换背景ajax提交
	 */
	public function changeBgAction() {
		$id = I('id', 0, "intval"); // 微秀id
		$tplId = I('tplId', 0, "intval"); // 模板id
		$myShowTpl = M("MyShowTpl a")->where("a.id=%d", $tplId)->find() or $this->ajaxError("背景不存在");
		false !== M("MyShow")->where("a.cid=%d and a.id=%d", $this->cid, $id)->setField("tplId", $tplId) and $this->ajaxSuccess("更换背景成功");
		$this->ajaxSuccess("更换背景失败");
	}
	
	/**
	 * 编辑内容ajax提交
	 */
	public function modifyPostAction() {
		$id = I('id', 0, "intval"); // 微秀id
		$title = I('title'); // 标题配置
		$config = I('config'); // 内容配置
		$myShow = M("MyShow a")->where("a.cid=%d and a.id=%d", $this->cid, $id)->field("a.*")->find();
		$myShow['config'] = unserialize($myShow['config'] );
		foreach ( $myShow['config'] as $k => &$v ) {
			if ( array_key_exists($k, $config) ) {
				if ( $v['type'] == 'text' ) {
					if ( empty($config[$k]['content']) ) {
						unset($myShow['config'][$k]);
					} else {
						$v['content'] = $config[$k]['content'];
					}
				} elseif ( $v['type'] == 'image' ) {
					if ( empty($config[$k]['url']) ) {
						unset($myShow['config'][$k]);
					} else {
						$v['url'] = $config[$k]['url'];
					}
				}
			} else {
				unset($myShow['config'][$k]);
			}
		}
		$myShow['title'] = $title;
		$myShow['config'] = serialize($myShow['config'] );
		false !== M("MyShow")->save($myShow) and $this->ajaxSuccess("修改成功");
		$this->ajaxError("修改失败");
	}
	
	/**
	 * 发布
	 */
	public function publishPostAction() {
		
	}
	
}